package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.upmp.dao.BatchExcpCfrmMapper;
import cc.rengu.redp.bizimpl.upmp.domain.BatchExcpCfrm;
import cc.rengu.redp.bizimpl.upmp.service.BatchExcpCfrmService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.RedpResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
  *	对BatchExcpCfrm实体提供服务
  *
  * @author zhuyaping
  * @date 2020年09月21日09时34分
  *
  *	@see 
  */
  
@Service
@Transactional
@DS("csmc")
public class BatchExcpCfrmServiceImpl extends ServiceImpl<BatchExcpCfrmMapper, BatchExcpCfrm> implements BatchExcpCfrmService {


    @Qualifier("upmpSysParamService")
    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Autowired
    private BatchExcpCfrmMapper batchExcpCfrmMapper;

    public IPage<?> findList(HttpServletRequest request) {

        String instId = "";
        UpmpUserInfoVO upmpUserInfoVO = CommonUtils.getCurrentUser(upmpSysParamService);
        if (null != upmpUserInfoVO && upmpUserInfoVO.isAdmin()) {
            instId = request.getParameter("instId").split("-")[0];
        } else {
            instId = upmpUserInfoVO.getInstId();
        }
        String batchDate = request.getParameter("batchDate");
        QueryWrapper<BatchExcpCfrm> queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(batchDate)){
            queryWrapper.eq("batch_date", batchDate);
        }
        queryWrapper.eq("inst_id", instId).orderByDesc("batch_date");

        Page<BatchExcpCfrm> page = new Page<BatchExcpCfrm>(Long.parseLong(request.getParameter("currentPage")),
                Long.parseLong(request.getParameter("pageSize")));
        batchExcpCfrmMapper.selectPage(page, queryWrapper);
        System.out.println(page);
        return page;
    }




    @Override
    public void saveData(HttpServletRequest request) {

        String  excpCfrmStatus = request.getParameter("excpCfrmStatus");
        String  instId = request.getParameter("instId");
        String  batchDate = request.getParameter("batchDate");
        String  batchNo = request.getParameter("batchNo");
        String  bizProdType = request.getParameter("bizProdType");
        String  excpCfrmType = request.getParameter("excpCfrmType");

        UpdateWrapper<BatchExcpCfrm> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("inst_id", instId).eq("batch_date", batchDate).eq("batch_no", batchNo)
                .eq("biz_prod_type", bizProdType).eq("excp_cfrm_type", excpCfrmType);
        updateWrapper.set("excp_cfrm_status", excpCfrmStatus);
        batchExcpCfrmMapper.update(null, updateWrapper);
    }
}
